<?php

namespace app\index\controller;

use think\cache\driver\Redis;
use think\Request;

use app\common\redis\RedisPackage;

class User extends Common
{
    //用户审核
    public function user_check()
    {
        if ($this->isCZY) {
            $user_id = $this->par_id;
        } else {
            $user_id = $this->id;
        }
        $lb = db('jujian')
            ->where('id', $user_id)
            ->field('par_id,id')
            ->find();
        if ($lb['par_id'] == 0) {
            $result = db('yonghu')
                ->where('bz', 0)
                ->where('sj_xulie', 'like', '%,' . $user_id . ',%')
                ->select();
            if (!empty($result)) {
                foreach ($result as $k => &$val) {
                    $result[$k]['bz'] = '未审核';
                    $result[$k]['beizhu'] = '';
                }
                $array['code'] = '1';
                $array['msg'] = '请求成功';
                $array['result'] = $result;
                return json_encode($array);
            } else {
                $array['code'] = '0';
                $array['msg'] = '暂无数据';
                return json_encode($array);
            }
        } else {
            //查询bz状态为0或者2的数据
            $map = array();
            $map['bz'] = array(['=', 0], ['=', 2], 'or');
            $result = db('yonghu')
                ->where($map)
                ->where('sj_xulie', 'like', '%,' . $user_id . ',%')
                ->select();
            if (!empty($result)) {
                foreach ($result as $k => &$val) {
                    if ($val['bz'] == 0) {
                        $result[$k]['bz'] = '未审核';
                    } else {
                        $result[$k]['bz'] = '已拒绝';
                    }
                    $result[$k]['beizhu'] = '';
                }
                $array['code'] = '1';
                $array['msg'] = '请求成功';
                $array['result'] = $result;
                return json_encode($array);
            } else {
                $array['code'] = '0';
                $array['msg'] = '暂无数据';
                return json_encode($array);
            }
        }
    }

    //单个用户审核
    public function user_check_sh()
    {
        if ($this->isCZY) {
            $par_id = $this->jujian['par_id'];
            $par = db('jujian')->where('id', $par_id)->find();
            if ($par['lb_id'] == 1) {
                $data['is_xianshi'] = '1';
            } else {
                $data['is_xianshi'] = '0';
            }
            $user_id = $this->id;
        } else {
            if ($this->is_Dingji) {
                $data['is_xianshi'] = '1';
            } else {
                $data['is_xianshi'] = '0';
            }

        }
        $user_id = $this->id;
        //基本数据
        $xz_id = input('xz/d');
        $yonghu = db('yonghu')->field('id,xingming,bh,beizhu,tel,sfz_bh,sheng_id,shi_id,bank_id,dizhi,bank_bh,fee_zu_id,baozheng_zu_id,fengkong_zu_id,huobi_zu_id,luyou_zu_id,chujin_zu_id,rujin_zu_id,jujian_id')->where('id', $xz_id)->find();
        if (!$yonghu) {
            returnError('用户不存在');
        }
        $pbh = db('jujian')->where('id', $yonghu['jujian_id'])->value('bh');//拿到上级编号
        $yonghu['par_bh'] = $pbh;
        //获取顶级的居间ID
        $gt_jujian_id = db('jujian')->where('id', $user_id)->value('gt_jujian_id');
        $data['sheng'] = db('sheng')->field('id,sheng')->select();
        $data['shi'] = db('shi')->where('sheng_id', $yonghu['sheng_id'])->field('id,shi')->select();
        $data['bank'] = db('bank')->field('id,name')->select();
        $data['fee_yonghu_zu'] = db('fee_yonghu_zu')->where('ib_id', $gt_jujian_id)->field('id,name')->select();
        $data['baozheng_zu'] = db('baozheng_zu')->where('ib_id', $gt_jujian_id)->field('id,name')->select();
        $data['fengkong_moban'] = db('fengkong_moban')->where('ib_id', $gt_jujian_id)->field('id,name')->select();
        $data['huobi_zu'] = db('huobi_zu')->where('ib_id', $gt_jujian_id)->field('id,name')->select();
        $data['luyou_zu'] = db('luyou_zu')->where('ib_id', $gt_jujian_id)->field('id,name')->select();
        $data['chujin_zu'] = db('chujin_zu')->where('ib_id', $gt_jujian_id)->field('id,name')->select();
        $data['rujin_zu'] = db('rujin_zu')->where('ib_id', $gt_jujian_id)->field('id,name')->select();
        $data['yonghu'] = $yonghu;


        returnSuccess($data);
    }

    //判断用户审核
    public function user_check_panduan()
    {
        $data = input();
        if ($this->isCZY) {
            $user_id = $this->par_id;
        } else {
            $user_id = $this->id;
        }
        $par = db('jujian')->where('id', $user_id)->find();
        if (!empty($par)) {
            $end['code'] = '1';
            $end['msg'] = '获取数据成功';
            $end['leader_id'] = $par['par_id'];
        } else {
            $end['code'] = '0';
            $end['msg'] = '未查询到数据';
            $end['leader_id'] = '';
        }
        return json_encode($end);
    }

    //用户审核通过
    public function user_check_tongguo()
    {
        $data = input();
        $sxfmb = input('sxfmb/d');
        if (!$sxfmb) {
            returnError('请选择手续费模板');
        }

        $bzjmb = input('bzjmb/d');
        if (!$bzjmb) {
            returnError('请选择保证金模板');
        }

        $fkmb = input('fkmb/d');
        if (!$fkmb) {
            returnError('请选择风控模板');
        }

        $hlmb = input('hlmb/d');
        if (!$hlmb) {
            returnError('请选择汇率模板');
        }

        $lyz = input('lyz/d');
        if (!$lyz) {
            returnError('请选择路由模板');
        }

        $chujinzu = input('chujinzu/d');
        if (!$chujinzu) {
            returnError('请选择出金模板');
        }
        $rujinzu = input('rujinzu/d');
        if (!$rujinzu) {
            returnError('请选择入金模板');
        }
        if (input('chongshen/d') == 0) {
            return json_encode(['code' => 0, 'msg' => '您没有权限审核']);
        }
        if (input('xz/d') == 0) {
            return json_encode(['code' => 0, 'msg' => '请选择要审核的用户']);
        }
        $shuju = array(
            'bz' => 1
        , 'fee_zu_id' => $sxfmb
        , 'baozheng_zu_id' => $bzjmb
        , 'fengkong_zu_id' => $fkmb
        , 'huobi_zu_id' => $hlmb
        , 'luyou_zu_id' => $lyz
        , 'rujin_zu_id' => $rujinzu
        , 'chujin_zu_id' => $chujinzu
        );
        if ($data['chongshen'] == 0) {
            //代理不是最高级代理，没有通过审核权限
            //$data['xz']为yonghu表的id
            $us = db('yonghu')->where('id', $data['xz'])->update(['bz' => 0]);
            if ($us) {
                $res['code'] = 1;
                $res['msg'] = 'success';
            } else {
                $res['code'] = 0;
                $res['msg'] = '用户重审提交失败';
            }
        } else {
            $user = db('yonghu')->where('id', $data['xz'])->find();
            $par = db('jujian')->where('id', $user['jujian_id'])->find();
            if (!$par) {
                returnError('编辑的用户上级不存在');
            }
            $user_shouxufei_muban = db('fee_yonghu_zu')->where('id', $sxfmb)->find();
            if (!$user_shouxufei_muban) {
                returnError('手续费模板不存在');
            }
            if ($par['lb_id'] != 1 && $user['fee_zu_id'] != $sxfmb) {
                $user_heyues = db('fee_yonghu_zu_detail')->where('fee_zu_id', $sxfmb)->select();
                foreach ($user_heyues as $k => $v) {
                    $max = db('jujian')
                        ->alias('j')
                        ->where('j.id', $user['jujian_id'])
                        ->join('fee_jujian_zu_detail fjd', 'fjd.fee_zu_id = j.fee_zu_id and fjd.heyue_zu_id = ' . $v['heyue_zu_id'], 'LEFT')
                        ->join('fee_jujian_zu fj', 'fj.ib_id = j.gt_jujian_id and fj.moren_bz = 1', 'LEFT')
                        ->join('fee_jujian_zu_detail fjd1', 'fjd1.fee_zu_id = fj.id and fjd1.heyue_zu_id = ' . $v['heyue_zu_id'], 'LEFT')
                        ->field("if(`fjd`.`jian_yongjin`,`fjd`.`jian_yongjin`,`fjd1`.`jian_yongjin`) as jian_yongjin,if(`fjd`.`ping_zuo_yongjin`,`fjd`.`ping_zuo_yongjin`,`fjd1`.`ping_zuo_yongjin`) as ping_zuo_yongjin,if(`fjd`.`ping_jin_yongjin`,`fjd`.`ping_jin_yongjin`,`fjd1`.`ping_jin_yongjin`) as ping_jin_yongjin")
                        ->find();
                    if ($v['jian_yongjin'] < $max['jian_yongjin'] || $v['ping_zuo_yongjin'] < $max['ping_zuo_yongjin'] || $v['ping_jin_yongjin'] < $max['ping_jin_yongjin']) {
                        returnError("错误：当前手续费模板合约费率（建：{$v['jian_yongjin']}平昨：{$v['ping_zuo_yongjin']} 平今：{$v['ping_zuo_yongjin']}）小于上级居间费率（建：{$max['jian_yongjin']}平昨：{$max['ping_zuo_yongjin']} 平今：{$max['ping_zuo_yongjin']}）");
                    }
                }
            }
            $result = db('yonghu')->where('id', $data['xz'])->update($shuju);
            if (!db('yonghu_bian')->where('yh_id', $data['xz'])->find()) {
                //货币
                $yonghu_bian_shuju = array(['yh_id' => $data['xz'], 'hb_id' => 1], ['yh_id' => $data['xz'], 'hb_id' => 2], ['yh_id' => $data['xz'], 'hb_id' => 3], ['yh_id' => $data['xz'], 'hb_id' => 4]);
                $result = db('yonghu_bian')->insertAll($yonghu_bian_shuju);
            }


            if ($result) {
                $id = $this->id;
                $operation_category = 22;
                $detail_name = array('用户审核通过', '审核操作者ID', '用户ID', '用户编号', '用户姓名');
                $operation_value = array('成功', $id, $user['id'], $user['bh'], $user['xingming']);
                $re_name = 'user_check';
                $re_value = '1';
                $rizhi = new Rizhi();
                $rizhi->index($id, $operation_category, $detail_name, $operation_value, $re_name, $re_value);
                $res['code'] = 1;
                $res['msg'] = '用户审核成功';
            } else {
                $res['code'] = 0;
                $res['msg'] = '用户审核通过失败';
            }
        }
        return json_encode($res);
    }

    /**
     * 用户审核 代理编辑重申接口
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function usercongshen()
    {
        //-----------------------------------------验证参数-----------------------------------
        $id = input('id/d');
        if (!$id) {
            returnError('用户不存在');
        }
        //姓名
        $xm = input('xm');
        if (!$xm) {
            returnError('姓名不可为空');
        }
        //电话
        $dh = input('dh');
        if (!$dh) {
            returnError('电话不可为空');
        }
        //身份证号
        $sfzh = input('sfzh');
        if (!$sfzh) {
            returnError('身份证不可为空');
        }
        $sheng = input('sheng/d');
        if ($sheng == 0) {
            returnError('请选择省份');
        }

        $shi = input('shi/d');
        if ($shi <= 0) {
            returnError('请选择市');
        }

        $khh = input('khh/d');
        if ($khh <= 0) {
            returnError('请选择开户行');
        }

        $khhdz = input('khhdz');
        if (!$khhdz) {
            returnError('请输入开户地址');
        }

        $yhkh = input('yhkh');
        if (!$yhkh) {
            returnError('请输入银行卡号');
        }

        $user = db('yonghu')->where('id', $id)->find();
        if (!$user) {
            returnError('编辑的用户不存在');
        }
        //-----------------------------------------验证参数结束-----------------------------------
        $shuju = array(
            'xingming' => $xm
        , 'tel' => $dh
        , 'sfz_bh' => $sfzh
        , 'sheng_id' => $sheng
        , 'shi_id' => $shi
        , 'bank_id' => $khh
        , 'dizhi' => $khhdz
        , 'bank_bh' => $yhkh
        , 'bz' => 0
        );
        $bool = db('yonghu')->where('id', $id)->update($shuju);
        if ($bool) {
            returnSuccess('重申成功');
        } else {
            returnError('重申失败');
        }
    }


    //用户审核拒绝  2为拒绝
    public function user_check_jujue()
    {

        $data = input();
        if (input('xz/d') == 0) {
            return json_encode(['code' => 0, 'msg' => 'xz参数错误']);
        }
        $yh = model('Yonghu');
        $result = $yh->allowField(true)->save(['bz' => '2'], ['id' => $data['xz']]);
        $yonghu = $yh->where('id', $data['xz'])->find()->toArray();
        if ($result) {
            $user_id = $this->id;
            $operation_category = 23;
            $detail_name = array('用户审核驳回', '审核操作者ID', '用户ID', '用户编号', '用户姓名');
            $operation_value = array('驳回', $user_id, $yonghu['id'], $yonghu['bh'], $yonghu['xingming']);
            $re_name = 'user_check_refuse';
            $re_value = '1';
            $rizhi = new Rizhi();
            $rizhi->index($user_id, $operation_category, $detail_name, $operation_value, $re_name, $re_value);
            $res['code'] = 1;
            $res['msg'] = '用户驳回成功';
        } else {
            $res['code'] = 0;
            $res['msg'] = '用户驳回失败';
        }
        return json_encode($res);
    }

    //用户审核删除
    public function user_check_delete()
    {
        $data = input();
        if (input('xz/d') == 0) {
            return json_encode(['code' => 0, 'msg' => 'xz参数错误']);
        }
        $result = db('yonghu')->where('id', $data['xz'])->update(['bz' => 3]);
        if ($result) {
            $res['code'] = 1;
            $res['msg'] = '删除成功';
        } else {
            $res['code'] = 0;
            $res['msg'] = '删除失败';
        }
        return json_encode($res);

    }

    public function zxyh()
    {
        if ($this->isCZY) {
            $dl_yh_id = $this->par_id;
        } else {
            $dl_yh_id = $this->id;
        }
        $jieguo = db('zaixian')
            ->alias('z')
            ->join('yonghu y', 'z.yh_id = y.id', 'LEFT')
            ->where('z.lb', 1)
            ->where('y.sj_xulie', 'like', "%," . $dl_yh_id . ",%")
            ->field('z.*,y.bh,y.xingming')
            ->select();
        foreach ($jieguo as $k=>$v){
            if(config('web_name') == 'HC'){
                if(!$this->is_Dingji){
                    if(preg_match("/^1[345678]{1}\d{9}$/", $jieguo[$k]['bh'])){
                        $jieguo[$k]['bh'] = substr_replace( $jieguo[$k]['bh'],'****',3,4);
                    }
                }
            }

        }
        if (!empty($jieguo)) {
            $res['code'] = 1;
            $res['msg'] = '请求成功';
            $res['result'] = $jieguo;
        } else {
            $res['code'] = 0;
            $res['msg'] = '暂无数据';
            $res['result'] = '';
        }
        return json_encode($res);
    }

    //用户操作日志
    public function yhczrz(Request $request)
    {
        $data = input();
        if ($this->isCZY) {
            $dl_yh_id = $this->par_id;
        } else {
            $dl_yh_id = $this->id;
        }
        if (empty($data['date_start'])) {
            $date_start = strtotime(date('Y-m-d 00:00:00', time()));
        } else {
            $date_start = strtotime($data['date_start']);
        }
        if (empty($data['date_end'])) {
            $date_end = strtotime(date('Y-m-d 23:59:59', time()));
        } else {
            $date_end = strtotime('+23 hours 59 minute 59 seconds', strtotime($data['date_end']));
        }
        if (empty($data['name'])) {
            $data['name'] = '';
        } else {
            $name = trim($data['name']);
        }
        if (empty($data['czzl'])) {
            $data['czzl'] = '';
        } else {
            $czzl = $data['czzl'];
        }
        if (empty($data['qujian'])) {
            $sonJujianId = $data['qujian'] = '';
        } else {
            $sonJujianId = $data['qujian'];
        }
        $or = '';
        if ($sonJujianId) {
            $sonJujianId = trim($sonJujianId, ',');
            $sonJujianId = explode(',', $sonJujianId);
            if (count($sonJujianId) >= 1) {
                if (count($sonJujianId) == 1) {
                    //判断选择的是不是自己
                    if ($sonJujianId[0] == $this->id) {
                        $where['y.jujian_id'] = $sonJujianId[0];
                    } else {
                        $where['y.sj_xulie'] = ['like', "%,$sonJujianId[0],%"];
                    }
                } else {
                    foreach ($sonJujianId as $k => $v) {
                        if ($k == 0) {
                            $or .= " y.sj_xulie like '%,$v,%'";
                        } else {
                            $or .= " or y.sj_xulie like '%,$v,%'";
                        }
                    }
                }
            }
        } else {
            $where['y.sj_xulie'] = ['like', "%,$dl_yh_id,%"];
        }

        $where['or.operation_time'] = ['between time', [(int)$date_start, (int)$date_end]];
        $where['or.user_category'] = 1;
        if (!empty($name)) {
            $where['y.bh|y.xingming|y.tel'] = ['like', "%" . $name . "%"];
        }
        if (!empty($czzl)) {
            $czzl_id = db('operation_records_category')->where('name', $czzl)->value('id');
            $where['or.operation_category'] = ['like', "%" . $czzl_id . "%"];
        }
        $result_operation_records_num = db('operation_records')
            ->alias('or')
            ->join('yonghu y', 'y.id=or.user_id')
            ->join('operation_records_category rc', 'rc.id=or.operation_category', 'LEFT')
            ->where($where)
            ->where($or)
            ->field('or.id,or.user_id,or.ip,or.operation_time,y.sj_xulie,y.bh,y.xingming,rc.name as czzl')
            ->order('operation_time asc')
            ->count();
        $page = input('page/d') ? input('page/d') : 1;
        $limit = input('limit/d') ? input('limit/d') : 20;
        $result_operation_records = db('operation_records')
            ->alias('or')
            ->join('yonghu y', 'y.id=or.user_id')
            ->join('operation_records_category rc', 'rc.id=or.operation_category', 'LEFT')
            ->where($where)
            ->where($or)
            ->field('or.id,or.user_id,or.ip,or.operation_time,y.sj_xulie,y.bh,y.xingming,rc.name as czzl')
            ->order('operation_time asc')
            ->page($page, $limit)
            ->select();
        foreach ($result_operation_records as $k => $v) {
            $result_operation_records[$k]['dailiguanxi'] = $this->get_dl_name($v['sj_xulie']);
        }
        $result_count = count($result_operation_records);
        foreach ($result_operation_records as $k=>$v){
            if(config('web_name') == 'HC'){
                if(!$this->is_Dingji){
                    if(preg_match("/^1[345678]{1}\d{9}$/", $result_operation_records[$k]['bh'])){
                        $result_operation_records[$k]['bh'] = substr_replace( $result_operation_records[$k]['bh'],'****',3,4);
                    }
                }
            }
        }
        $jieguo['num'] = $result_operation_records_num;
        $jieguo['result'] = $result_operation_records;

        if (!empty($result_count)) {
            returnSuccess(['data' => $jieguo]);
            die;
        } else {
            returnError(['data' => '', 'msg' => '暂无数据']);
        }
    }

    //用户操作详情
    public function user_operation_details(Request $request)
    {
        $data = input();
        if (input('id/d') == 0) {
            $res['code'] = '0';
            $res['msg'] = 'ID不能为空';
            return json_encode($res);
        }
        $result = db('operation_records_detail')->where('par_id', $data['id'])->order('id desc')->select();
        if (!empty($result)) {
            $res['code'] = 1;
            $res['msg'] = '查询详情成功';
            $res['result'] = $result;
        } else {
            $res['code'] = 0;
            $res['msg'] = '查询详情失败';
            $res['result'] = '';
        }
        return json_encode($res);
    }

    //用户回收站
    public function user_bin()
    {
        $gt_jujian_id = $this->jujian['gt_jujian_id'];
        $requert = db('yonghu')->where('gt_jujian_id', $gt_jujian_id)->where('bz', 3)->select();
        if (!empty($requert)) {
            $res['code'] = 1;
            $res['msg'] = '查询成功';
            $res['result'] = $requert;
        } else {
            $res['code'] = 0;
            $res['msg'] = '暂无数据';
            $res['result'] = '';
        }
        return json_encode($res);
    }

    //用户回收站恢复
    public function user_bin_restore(Request $request)
    {
        $data = input();
        if (input('bj_id/d') == 0) {
            $res['code'] = '0';
            $res['msg'] = '用户bj_id不能为空';
            return json_encode($res);
        }
        $result = db('yonghu')->where('id', $data['bj_id'])->update(['bz' => 0]);
        if ($result) {
            $res['code'] = 1;
            $res['msg'] = '操作成功';
        } else {
            $res['code'] = 0;
            $res['msg'] = '操作失败';
        }
        return json_encode($res);
    }
}

?>